import { createRouter, createWebHistory } from 'vue-router'
import { useTokenStore } from '@/stores/token'

const routes = [
  {
    path:'/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/register',
    component: () => import('@/views/Register.vue')
  },
  {
    path: '/production',
    component: () => import('@/views/ProductionLine.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/device/list',
    component: () => import('@/views/device/DeviceList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/process/list',
    component: () => import('@/views/process/ProcessTaskList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/process/addtask',
    component: () => import('@/views/process/ProcessTaskAdd.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/process/device',
    component: () => import('@/views/process/ProcessDevice.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/line/list',
    component: () => import('@/views/line/ProductionLineList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/line',
    component: () => import('@/views/line/ProductionLineList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/line/free',
    component: () => import('@/views/line/freeLineList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/order/list',
    component: () => import('@/views/order/OrderList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/line/stop-order',
    component: () => import('@/views/line/StopOrderList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/process/history',
    component: () => import('@/views/process/ProcessHistory.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/process/parameter',
    component: () => import('@/views/process/ProcessParameter.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes : routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const tokenStore = useTokenStore()

  if (to.meta.requiresAuth && !tokenStore.token&&localStorage.getItem('token')) {
    next('/login')
  }
  else {
    next()
  }
})

export default router
